package com.example.cofeed.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.cofeed.R;
import com.example.cofeed.adapter.DateSelectAdapter;
import com.example.cofeed.adapter.MySubscriptionAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.Areas;
import com.example.cofeed.entity.Catalogs;
import com.example.cofeed.entity.City;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MySubscriptionActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private MySubscriptionAdapter mySubscriptionAdapter;
    private List<Catalogs> catalogsList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_subscription;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("我的订阅");

        mySubscriptionAdapter = new MySubscriptionAdapter();
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRvList.setAdapter(mySubscriptionAdapter);

        mRvList.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this)
                        .color(Color.TRANSPARENT)
                        .sizeResId(R.dimen.dp_10)
                        .build());

        mySubscriptionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Catalogs catalogs = (Catalogs) adapter.getData().get(position);
                mySubscriptionAdapter.setSelect(catalogs);

            }
        });

        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            getData();

        });

        getData();

    }

    private void getData() {
        ApiService.my_subscription(this, new DialogCallback<LzyResponse<List<Catalogs>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Catalogs>>> response) {
                if (!ObjectUtils.isEmpty(response.body().data)) {
                    catalogsList.addAll(response.body().data);
                }
                mySubscriptionAdapter.setNewData(catalogsList);
            }
        });
    }


    @OnClick(R.id.layout_renew)
    public void onViewClicked() {
        showDateDialog();

    }

    int dateSelectPosition = 3;
    private void showDateDialog() {
        final Dialog mDialog = new Dialog(this, R.style.CustomDialog);
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_date_select, null, false);
        TextView breedTv = (TextView) inflate.findViewById(R.id.breed_tv);
        breedTv.setText("订阅时间");
        inflate.findViewById(R.id.cancel_tv).setVisibility(View.VISIBLE);
        inflate.findViewById(R.id.cancel_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        TextView confirmTv = (TextView) inflate.findViewById(R.id.confirm_tv);
        confirmTv.setText("结算");

        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        RecyclerView rvList = (RecyclerView) inflate.findViewById(R.id.rec_lv);
        mDialog.setContentView(inflate);
        mDialog.setCancelable(true);
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Areas> areasList = new ArrayList<>();
                for (Catalogs catalogs:mySubscriptionAdapter.getData()){

                    for (City city:catalogs.citys){
                        if (city.ispush == 1){
                            Areas areas = new Areas();
                            switch (dateSelectPosition){
                                case 0:
                                    areas.buytime = "1";
                                    break;
                                case 1:
                                    areas.buytime = "3";
                                    break;
                                case 2:
                                    areas.buytime = "6";
                                    break;
                                case 3:
                                    areas.buytime = "12";
                                    break;
                                case 4:
                                    areas.buytime = "24";
                                    break;
                            }
                            areas.catalogid = catalogs.catalogid;
                            areas.cityid = city.cityid;
                            areasList.add(areas);
                        }
                    }
//                    if (catalogs.isSelect){
//                        for (City city:catalogs.citys){
//                            if (city.ispush == 1){
//                                areas.catalogid = catalogs.catalogid;
//                                areas.cityid = city.cityid;
//                                areasList.add(areas);
//                            }
//                        }
//
//                    }
                }

                if (ObjectUtils.isEmpty(areasList)){
                    showToast("您还未选择");
                    return;
                }

                ApiService.orders(this, "", areasList, new DialogCallback<LzyResponse<Map<String, String>>>(MySubscriptionActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<Map<String, String>>> response) {
                        String money = response.body().data.get("money");
                        if (!ObjectUtils.isEmpty(money)){
                            if (money.equals("0")){
                                showToast("订阅成功!");
                                return;
                            }
                        }
                        Intent intent = new Intent(MySubscriptionActivity.this, PayActivity.class);
                        intent.putExtra(SputilsConstant.ORDER_ID, response.body().data.get("orderno"));
                        intent.putExtra(SputilsConstant.MONEY, money);
                        startActivityForResult(intent, 3);
                    }
                });

                mDialog.dismiss();
            }
        });

        rvList.setLayoutManager(new GridLayoutManager(this, 5));
        final DateSelectAdapter dateSelectAdapter = new DateSelectAdapter();
        rvList.setAdapter(dateSelectAdapter);

        dateSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                dateSelectPosition = position;
                dateSelectAdapter.setSelect(position);


            }
        });

        List<String> breeds = new ArrayList<>();
        breeds.add("一个月");
        breeds.add("三个月");
        breeds.add("半年");
        breeds.add("一年");
        breeds.add("两年");

        dateSelectAdapter.setNewData(breeds);
        dateSelectAdapter.setSelect(3);

        Window localWindow = mDialog.getWindow();
        localWindow.setBackgroundDrawableResource(android.R.color.transparent);
        localWindow.setGravity(Gravity.BOTTOM);
        localWindow.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        if (mDialog != null && !mDialog.isShowing()){
            mDialog.show();
            mDialog.setCanceledOnTouchOutside(true);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
